package Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class JDBCUtils {
	
	// 加载驱动，并建立数据库连接
	public static Connection getConnection() {
		
		Connection conn = null;
			try {
				
				Class.forName("com.mysql.jdbc.Driver");
				String url = "jdbc:mysql://localhost:3306/t18jdbc";
				String username = "root";
				String password = "123456";
				conn= DriverManager.getConnection(url,username,password);
				
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return conn;
		}
	
	
		// 关闭数据库连接，释放资源
		public static void release(Statement st, Connection conn) {
			if (st != null) {
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				st = null;
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				conn = null;
			}
		}
		
	     public static void release(ResultSet rs, Statement st, 
	     		Connection conn){
	    	 
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				rs = null;
			}
			release(st, conn);
		}
}
